﻿Public Class RoleProvider
    Inherits System.Web.Security.RoleProvider
    Implements IRoleProvider

#Region " Private Members "

    Private _service As New RoleProviderService.RoleProvider()
    Private _applicationName As String
    Private _remoteProviderName As String

#End Region

#Region " Private Methods "

    Private Sub PrepareConfigFromWebService(ByVal config As System.Collections.Specialized.NameValueCollection)

        If config("remoteProviderName") Is Nothing OrElse config("remoteProviderName").ToString.Length = 0 Then
            Throw New Exception("remoteProviderName attribute did not set!")
        Else
            _remoteProviderName = config("remoteProviderName")
        End If

        If config("roleProviderUri") IsNot Nothing AndAlso config("roleProviderUri").ToString.Length > 0 Then
            _service.Url = config("roleProviderUri")
        End If

        Dim providerSettings() As RoleProviderService.ProviderSetting = _service.GetProviderSettings(RemoteProviderName, ApplicationName)

        If config Is Nothing Then
            config = New System.Collections.Specialized.NameValueCollection
        End If

        For Each providerSetting As RoleProviderService.ProviderSetting In providerSettings
            If config(providerSetting.Name) Is Nothing Then
                config.Add(providerSetting.Name, providerSetting.Value)
            Else
                config(providerSetting.Name) = providerSetting.Value
            End If
        Next

        If config("applicationname") Is Nothing Then
            config.Add("applicationname", "/")
        End If

        If config("applicationname").ToString.Length = 0 Then
            _applicationName = "/"
            config("applicationname") = "/"
        Else
            _applicationName = config("applicationname")
        End If

    End Sub

#End Region

#Region " Public Properties "

    Public Overrides Property ApplicationName() As String Implements IRoleProvider.ApplicationName
        Get
            Return _applicationName
        End Get
        Set(ByVal value As String)
            _applicationName = value
        End Set
    End Property

    Public ReadOnly Property RemoteProviderName() As String Implements IRoleProvider.RemoteProviderName
        Get
            Return _remoteProviderName
        End Get
    End Property

#End Region

#Region " Public Methods "

    Public Sub New()
        MyBase.New()
    End Sub

    Public Overrides Sub Initialize(ByVal name As String, ByVal config As System.Collections.Specialized.NameValueCollection) Implements IRoleProvider.Initialize

        PrepareConfigFromWebService(config)

        MyBase.Initialize(name, config)

    End Sub

    Public Overrides Sub AddUsersToRoles(ByVal usernames() As String, ByVal roleNames() As String) Implements IRoleProvider.AddUsersToRoles
        _service.AddUsersToRoles(_remoteProviderName, _applicationName, usernames, roleNames)
    End Sub

    Public Overrides Sub CreateRole(ByVal roleName As String) Implements IRoleProvider.CreateRole
        _service.CreateRole(_remoteProviderName, _applicationName, roleName)
    End Sub

    Public Overrides Function DeleteRole(ByVal roleName As String, ByVal throwOnPopulatedRole As Boolean) As Boolean Implements IRoleProvider.DeleteRole
        Return _service.DeleteRole(_remoteProviderName, _applicationName, roleName, throwOnPopulatedRole)
    End Function

    Public Overrides Function FindUsersInRole(ByVal roleName As String, ByVal usernameToMatch As String) As String() Implements IRoleProvider.FindUsersInRole
        Return _service.FindUsersInRole(_remoteProviderName, _applicationName, roleName, usernameToMatch)
    End Function

    Public Overrides Function GetAllRoles() As String() Implements IRoleProvider.GetAllRoles
        Return _service.GetAllRoles(_remoteProviderName, _applicationName)
    End Function

    Public Overrides Function GetRolesForUser(ByVal username As String) As String() Implements IRoleProvider.GetRolesForUser
        Return _service.GetRolesForUser(_remoteProviderName, _applicationName, username)
    End Function

    Public Overrides Function GetUsersInRole(ByVal roleName As String) As String() Implements IRoleProvider.GetUsersInRole
        Return _service.GetUsersInRole(_remoteProviderName, _applicationName, roleName)
    End Function

    Public Overrides Function IsUserInRole(ByVal username As String, ByVal roleName As String) As Boolean Implements IRoleProvider.IsUserInRole
        Return _service.IsUserInRole(_remoteProviderName, _applicationName, username, roleName)
    End Function

    Public Overrides Sub RemoveUsersFromRoles(ByVal usernames() As String, ByVal roleNames() As String) Implements IRoleProvider.RemoveUsersFromRoles
        _service.RemoveUsersFromRoles(_remoteProviderName, _applicationName, usernames, roleNames)
    End Sub

    Public Overrides Function RoleExists(ByVal roleName As String) As Boolean Implements IRoleProvider.RoleExists
        Return _service.RoleExists(_remoteProviderName, _applicationName, roleName)
    End Function

#End Region

End Class
